<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>顶牛BBD</title>
		<link href="css/text.css" rel="stylesheet" type="text/css" />
		<style type="text/css">				
			//p {margin:5px}
			//ul {margin:1px}			
			//h4 {margin:1px}	
			.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}
			.res
			{
				color: Red;
			}
			.result{
				background: Yellow;
			}			
		</style>		
		<script type="text/javascript" src="js/json2html.js"></script>
		<script type="text/javascript" src="js/jquery.min.js"></script>
		<script>
			var text = "";
			window.onload = function() {
			text = load("A股代码.txt");			
			//document.write(text);	
			getSource("6");
			changeStyle();
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					getSource("6");					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				time_range("9:00","15:15")						
				},
				60000);
			})
			
			function changeStyle() {
				var obj1 = document.getElementById("towrite");				
				obj1.style.left= "0px"; 				
				obj1.style.width= "480px";	
				obj1.style.height= "620px";		
				obj1.style.fontSize="12px";			
			}
			
			function createXmlHttp() {
				if (window.XMLHttpRequest) {
				xmlHttp = new XMLHttpRequest();
				} else {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
			}
					
			function changeCss(obj,id){				
				var alist = document.getElementById(id).getElementsByTagName("a");					
				for(var i = 0; i < alist.length; i++){				
					if(alist[i] == undefined){
						break;
					}
					alist[i].style.color = "#d2d2d3"; //给所有a标签赋原色
				}
				obj.style.color = "#00FFFF"; //令当前标签高亮				
			}
			
			function load(name) {
				var xhr = new XMLHttpRequest()							
				okStatus = document.location.protocol === "file:" ? 0 : 200;				
				xhr.open('GET', name, false);
				xhr.overrideMimeType("text/html;charset=utf-8");//默认为utf-8				
				xhr.send(null);
				return xhr.status === okStatus ? xhr.responseText : null;
			}

			var gl = "";  //高亮
			function getSource(s) {						
				div = document.getElementById('towrite');				
				var p1 = "<a href='javascript:void(0)' id='px2' onclick='getSource(" + "\"2\"" + ")'>" + "涨幅" + "</a>";
				var p2 = "<a href='javascript:void(0)' id='px6' onclick='getSource(" + "\"6\"" + ")'>" + "BBD" + "</a>";
				var p3 = "<a href='javascript:void(0)' id='px8' onclick='getSource(" + "\"8\"" + ")'>" + "通吃率" + "</a>";
				var p4 = "<a href='javascript:void(0)' id='px9' onclick='getSource(" + "\"9\"" + ")'>" + "DDX" + "</a>";
				var p5 = "<a href='javascript:void(0)' id='px19' onclick='getSource(" + "\"19\"" + ")'>" + "单数比" + "</a>";
				div.innerHTML = '<h2>顶牛BBD</h2>' + '<h4>（排序：' + p1 + p2 + p3 + p4 + p5 + '）</h4>';
				
				aObj = document.getElementById('px' + s);
				gl = aObj.innerText;
				aObj.innerText = aObj.innerText + "↓";
				aObj.style.color = "#FFA07A";
				
				var url = "http://www.dingniugu.com/ddxdata/ddedatatop.php?pid=" + s + "&p=500&order=-1"; 				
				createXmlHttp();
				xmlHttp.onreadystatechange = writeSource;
				xmlHttp.open("GET", url, true);
				xmlHttp.send(null);
			}
			
			var tt = "";
			function writeSource() { 
			if (xmlHttp.readyState == 4) { 					
				var data = xmlHttp.responseText
				//document.write(data);
				tt = data.split('var ddx_update=')[1].replace("'","(").replace("';",")");
				//document.write(tt);
				data = data.replace(/ /g,"").replace("varmyArray=newArray(","{\"List\":[").replace(")","]}");
				data = data.replace(/\'/g,"\"").split(";varddx")[0];
				data = data.replace(/(\[\").*?(?=\])/g,function(word){ 
				str = word.match(/\[(\S*)/)[1]; 
				return '{"code":' + str.split(',')[0] + 
				',"zf":' + str.split(',')[2] + 
				',"BBD":'+str.split(',')[6] + 
				',"tcl":'+str.split(',')[8] + 
				',"DDX":'+str.split(',')[9] +
				',"DDY":'+str.split(',')[10] +
				',"DDZ":'+str.split(',')[11] +
				',"tcl":'+str.split(',')[8] +
				',"red":'+str.split(',')[14] +
				',"tddc":'+str.split(',')[15] +
				',"ddc":'+str.split(',')[16] +
				',"zdc":'+str.split(',')[17] +
				',"xdc":'+str.split(',')[18] +
				',"dsb":'+str.split(',')[19];
				});
				data = data.replace(/\],\{/g,"},{").replace(/\]\]/g,"}]");				
				//document.write(data);

				var transform_aa = [           
				{"tag":"ul", children:function(){return(json2html.transform(this.List, transform_bb));}}
				];
				
				var transform_bb= 
				{"tag":"p", children:[
				{"tag":"p", children:[							
				//{"tag": "li", "html":"<a href='http://www.treeid/code_${code}' >${code} &nbsp;涨幅：${zf}%&nbsp;BBD：${BBD}元&nbsp;通吃率：${tcl}&nbsp;DDX：${DDX}&nbsp;单数比：${dsb}</a>"}, 							
				{"tag": "li", "html": function(){
					var dm = this.code;
					var mc = dm;
					if(text.indexOf(dm) > -1)
					{				
						mc = text.split(dm+"|")[1]					
						mc = mc.split("\r\n")[0]					
					}
					
						return("<a href=http://www.treeid/code_" + dm + " id='gg' onclick='changeCss(this,this.id)'>" + 
						dm + " &nbsp;" +
						mc + " &nbsp;涨幅：" + this.zf + "% " +
						" &nbsp;BBD："+ this.BBD + "元 &nbsp;通吃率：" + this.tcl + 
						" &nbsp;DDX：" + this.DDX + " &nbsp;单数比：" + this.dsb +
						"</a>");
					}},				
				]}]};  
				

				var html = json2html.transform(data, transform_aa);		
				div = document.getElementById('towrite');				
				div.innerHTML = div.innerHTML + "<b>&nbsp;&nbsp;" + tt + "</b>" + "<span id='gg'>" + html +"</span>";
				
				//高亮
				var content = document.getElementById("gg").innerHTML;
				//var s = "三合一";
				var reg = new RegExp("(" + gl + ")", "g");  
				var str = content;  
				var newstr = str.replace(reg, "<font color=#FF6633>$1</font>");
				document.getElementById("gg").innerHTML = newstr;
				} 
			}
		</script>
		<script>
			var oldKey = "";
			var index = -1;
			var pos = new Array();
			var oldCount = 0;

			<!-- function previous(){ -->
			<!-- index--; -->
			<!-- index = index < 0 ? oldCount - 1 : index; -->
			<!-- search(); -->
			<!-- } -->
			function gotop(){							
				$(".fixedBox").scrollTop(0);
			}
			
			function next(){			
			index++;
			//index = index == oldCount ? 0 : index;
			if(index==oldCount){
				index = 0 ;
			}
			search();
			}

			function search() {
				$(".result").removeClass("res");
				var key = $("#key").val(); 
				//alert(key);
				if (!key) {
					console.log("key为空则退出");
					$(".result").each(function () {
					$(this).replaceWith($(this).html());
					});
					oldKey = "";
					return; 
				}
				//if (oldKey != key) {
				//alert(key);
					console.log("进入重置方法");
					//重置
					$(".fixedBox").scrollTop(0);
					index = 0;
					$(".result").each(function () {
					$(this).replaceWith($(this).html());
					});
					pos = new Array();
					var regExp = new RegExp(key+'(?!([^<]+)?>)', 'ig');//正则表达式匹配
					//$("body").html($("body").html().replace(regExp, "<span id='result" + index + "' class='result'>" + key + "</span>")); // 高亮操作
					$(".fixedBox").html($(".fixedBox").html().replace(regExp, "<span id='result" + index + "' class='result'>" + key + "</span>")); // 高亮操作
					$("#key").val(key);
					oldKey = key;
					$(".result").each(function () {
					pos.push($(this).offset().top);
					});
					oldCount = $(".result").length;
					console.log("oldCount值：",oldCount);
				//}

				$(".result:eq(" + index + ")").addClass("res");
				$(".fixedBox").scrollTop(pos[index] - 100);
			}
		</script>	
	</head>
	<body>		
		<div id="towrite" class="fixedBox"></div>		
		<div style="position: fixed; left: 280px; top: 35;">
			<input id="key" type="text" style="width: 90px;"/>
			<input type="button" value="检索" style="font-family: Microsoft YaHei;" onclick="next()" />
			<input type="button" value="Top" style="font-family: Microsoft YaHei;" onclick="gotop()" />			
		</div>	
		<!-- <div id="towrite"></div> -->
	</body>
</html>